﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Net.NetworkInformation;
using System.Net;
using System.Security.Principal;
using System.Windows.Forms;
using DevExpress.XtraEditors.Repository;
using System.Collections;
using COMOSOFT.OrderBusiness.Forms;

namespace COMOSOFT.Class
{    
   public class ClassShare
    {      
       public static int Usid,fpid;
       public static string Usname, UserN, Pass,Email;
       static String ip = null;
       static String macAdd = null;

       public static String JobAsign = System.Configuration.ConfigurationManager.AppSettings["JobAsign"].ToString();
       public static String MAManagment_Image = System.Configuration.ConfigurationManager.AppSettings["MAManagment_Image"].ToString();
       public static String System_Image = System.Configuration.ConfigurationManager.AppSettings["System_Image"].ToString();
       public static String JobAsign_Image = System.Configuration.ConfigurationManager.AppSettings["JobAsign_Image"].ToString();
     
       String formname;
       //
        
       //var report
       public static string FPMIDs,OIDs;       
       //
       public ClassShare(string formname)
       {
           this.formname = formname;
       }
       //---------------------------
       //
       #region[Login]
       public SqlConnection connection()
       {
           SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=COMO;User ID=sa;Password=sa");
           //SqlConnection con = new SqlConnection(@"Data Source=192.168.1.100\SQLEXPRESS;Initial Catalog=COMO;User ID=COMO;Password=@Admin247");          
           if (con.State == ConnectionState.Closed)
           {
               con.Open();
           }
           return con;
       }
       internal bool checklogin(String user, string pass)
        {
            SqlCommand cmd = new SqlCommand("select * from Employee where account=@user and password=@pass", this.connection());
            cmd.Parameters.Add("@user", SqlDbType.VarChar);
            cmd.Parameters["@user"].Value = user;
            cmd.Parameters.Add("@pass", SqlDbType.VarChar);
            cmd.Parameters["@pass"].Value = pass;
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                UserN = user;
                Pass = pass;
                Usid = int.Parse(dr.GetSqlValue(0).ToString());
                Usname = dr.GetSqlValue(5).ToString();
                Email = dr.GetSqlValue(9).ToString();
                this.connection().Close();
                //createDirectory(directoryPath + UserN);
                //path = directoryPath + UserN + "/" + UserN + "_Mlog.txt";
                ip = GetIP();
                macAdd = getMacAddress();
                return true;
            }
            else
            {
                UserN = null;
                Pass = null;
                Usid = 0;
                MessageBox.Show("Please check agian,Cannot access systems!", "Login Fail!", MessageBoxButtons.OK, MessageBoxIcon.Warning);          
            this.connection().Close();
            return false;
            }
        }
       public string GetIP()
       {
           string strHostName = "";
           strHostName = Dns.GetHostName();
           IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
           IPAddress[] addr = ipEntry.AddressList;
           string cpname = WindowsIdentity.GetCurrent().Name.ToString();
           return " CompName:" + cpname + "***IP:" + addr[addr.Length - 1].ToString();
       }
       public string getMacAddress()
       {
           String macAddress = null;
           IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
           NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
           if (nics == null || nics.Length < 1)
           {
               return "";
           }
           NetworkInterface adapter = nics[0];
           PhysicalAddress address = adapter.GetPhysicalAddress();
           macAddress = "-MacAddress:" + address.ToString();
           return macAddress;
       }

       public DataTable CheckEmployee(int id)
       {
           SqlCommand command = new SqlCommand("sp_CheckEmployee", this.connection());
           command.CommandType = CommandType.StoredProcedure;
           command.Parameters.AddWithValue("@EID",id);
           command.ExecuteNonQuery();
           SqlDataAdapter adt = new SqlDataAdapter(command);
           DataTable dtb = new DataTable();
           adt.Fill(dtb);
           return dtb;
       }

       public DataTable GetData(string sql)
       {
           SqlCommand command = new SqlCommand(sql, this.connection());
           command.CommandType = CommandType.StoredProcedure;
           SqlDataAdapter adt = new SqlDataAdapter(command);
           DataTable dtb = new DataTable();
           adt.Fill(dtb);
           this.connection().Close();
           return dtb;
       }
       //update data
       public DataTable GetData(string sql, string[] name, object[] value)
       {
           SqlCommand command = new SqlCommand(sql, this.connection());
           command.CommandType = CommandType.StoredProcedure;
           for (int i = 0; i < name.Length; i++)
           {
               command.Parameters.AddWithValue(name[i], value[i]);
           }
           SqlDataAdapter adt = new SqlDataAdapter(command);
           DataTable dtb = new DataTable();
           adt.Fill(dtb);
           this.connection().Close();
           return dtb;
       }
       
       public int UpdateData(string sql)
       {
           SqlCommand command = new SqlCommand(sql, this.connection());
           command.CommandType = CommandType.StoredProcedure;
           int kq = command.ExecuteNonQuery();
           this.connection().Close();
           return kq;
       }
       public int UpdateData(string sql, string[] name, object[] value)
       {
           SqlCommand command = new SqlCommand(sql, this.connection());
           command.CommandType = CommandType.StoredProcedure;
           for (int i = 0; i < name.Length; i++)
           {
               command.Parameters.AddWithValue(name[i], value[i]);
           }
           int kq = command.ExecuteNonQuery();
           this.connection().Close();
           return kq;
       }
       //--------------------------------
    #endregion

       #region [DatasetToArrayList]
       internal ArrayList DataSetToArrayList(int ColumnIndex, DataTable dataTable)
       {
           ArrayList list = new ArrayList();
           foreach (DataRow row in dataTable.Rows)
           {
               list.Add(row[ColumnIndex]);
           }
           return list;
       }
       #endregion

       #region [SelectDistionct]
       internal DataTable SelectDistinct(DataTable SourceTable, params string[] FieldNames)
       {
           object[] lastValues;
           DataTable newTable;
           DataRow[] orderedRows;

           if (FieldNames == null || FieldNames.Length == 0)
               throw new ArgumentNullException("FieldNames");

           lastValues = new object[FieldNames.Length];
           newTable = new DataTable();

           foreach (string fieldName in FieldNames)
               newTable.Columns.Add(fieldName, SourceTable.Columns[fieldName].DataType);

           orderedRows = SourceTable.Select("", string.Join(", ", FieldNames));

           foreach (DataRow row in orderedRows)
           {
               if (!fieldValuesAreEqual(lastValues, row, FieldNames))
               {
                   newTable.Rows.Add(createRowClone(row, newTable.NewRow(), FieldNames));

                   setLastValues(lastValues, row, FieldNames);
               }
           }

           return newTable;
       }
       private static bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
       {
           bool areEqual = true;

           for (int i = 0; i < fieldNames.Length; i++)
           {
               if (lastValues[i] == null || !lastValues[i].Equals(currentRow[fieldNames[i]]))
               {
                   areEqual = false;
                   break;
               }
           }

           return areEqual;
       }
       private static DataRow createRowClone(DataRow sourceRow, DataRow newRow, string[] fieldNames)
       {
           foreach (string field in fieldNames)
               newRow[field] = sourceRow[field];

           return newRow;
       }

       private static void setLastValues(object[] lastValues, DataRow sourceRow, string[] fieldNames)
       {
           for (int i = 0; i < fieldNames.Length; i++)
               lastValues[i] = sourceRow[fieldNames[i]];
       }
       #endregion

       #region [AddingList]
       internal void AddingListRepCombo(RepositoryItemComboBox[] Ctl, DataTable tb, string[] ColName)
        {
            for (int i = 0; i < ColName.Length; i++)
            {
                ArrayList Drow = null;
                Drow = DataSetToArrayList(0, (SelectDistinct(tb, ColName[i])));
                Ctl[i].Items.Clear();
                Ctl[i].Items.AddRange(Drow);
            }
        }

        #endregion
    
    }  
}
